import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../components/LoginPage.vue'
import HomePage from '../components/HomePage.vue'
import Account from './AccountStore'
import MainPage from '../components/MainPage.vue'
import GoodsDetailPage from '../components/GoodsDetailPage.vue'
import ShopCar from '../components/ShopCar.vue'
import OrderPage from '../components/OrderPage.vue'
import SearchPage from '../components/SearchPage.vue'
import EvaluationPage from '../components/EvaluationPage.vue'
// 定义页面名称枚举
enum PageName {
  login = 'login',
  home = 'home'
}
// 定义路由
const shopRouter = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: PageName.login,
      component: LoginPage
    },
    {
      path: '/home',
      name: PageName.home,
      component: HomePage,
      children:[
        {
          path:'main',
          component:MainPage
        },
        {
          path:'goods/:id',
          component:GoodsDetailPage,
          name:"goodsDetail",
          props:true
        },
        {
          path:'shopCar',
          component:ShopCar,
          name:"shopCar"
        },
        {
          path:'order',
          component:OrderPage,
          name:"order"
        },
        {
          path:'search/:keyword',
          component:SearchPage,
          name:"search",
          props:true
        },
        {
          path:'evaluation/:order',
          component:EvaluationPage,
          name:"evaluation",
          props:true
        }
      ],
      redirect:"/home/main"
    }
  ]
});
// 定义前置守卫
shopRouter.beforeEach((to) => {
  // 如果访问的不是登录页，则重定向到登录页，如果没有登录则跳转到登录页
  if (to.name !== PageName.login && !Account().isLogin) {
    return {
      name: PageName.login
    }
  }
});
// 导出路由
export default shopRouter;
export {PageName}